<?php
/**
 * Created by PhpStorm.
 * Version :  v1.0.0
 * Date    :  2023/5/13 16:29
 */

use function Hyperf\Support\env;

return [
    'enable'    => [
        'discovery' => true,
        'register'  => true,
    ],
    'consumers' => value(function () {
        $consumers = [];
        // 服务列表
        $services = [
            "AuthService" => App\JsonRpc\AuthServiceInterface::class,
            "UserService" => App\JsonRpc\UserServiceInterface::class,
            "MerchantService" => App\JsonRpc\MerchantServiceInterface::class,
            "ManagerService" => App\JsonRpc\ManagerServiceInterface::class,
        ];
        foreach ($services as $name => $interface) {
            $consumers[] = [
                'name'          => $name,
                'service'       => $interface,
                // 对应容器对象 ID，可选，默认值等于 service 配置的值，用来定义依赖注入的 key
                'id'            => $interface,
                // 可选 jsonrpc-http jsonrpc jsonrpc-tcp-length-check
                'protocol'      => 'jsonrpc-http',
                // 负载均衡算法，可选，默认值为 random
                'load_balancer' => 'random',  // random,round-robin,weighted-random,weighted-round-robin
                // 这个消费者要从哪个服务中心获取节点信息，如不配置则不会从服务中心获取节点信息
                'registry'      => [
                    'protocol' => 'nacos',
                    'address'  => env('NACOS_URL', 'http://rpc.beedot.cn'),
                ],
                // 配置项，会影响到 Packer 和 Transporter
                'options'       => [
                    'connect_timeout' => 5.0,
                    'recv_timeout'    => 5.0,
                    'settings'        => [
                        // 根据协议不同，区分配置
                        'open_eof_split' => true,
                        'package_eof'    => "\r\n",
                        // 'open_length_check' => true,
                        // 'package_length_type' => 'N',
                        // 'package_length_offset' => 0,
                        // 'package_body_offset' => 4,
                    ],
                    // 重试次数，默认值为 2，收包超时不进行重试。暂只支持 JsonRpcPoolTransporter
                    'retry_count'     => 2,
                    // 重试间隔，毫秒
                    'retry_interval'  => 100,
                    // 使用多路复用 RPC 时的心跳间隔，null 为不触发心跳
                    'heartbeat'       => 30,
                    // 当使用 JsonRpcPoolTransporter 时会用到以下配置
                    'pool'            => [
                        'min_connections' => 1,
                        'max_connections' => 32,
                        'connect_timeout' => 10.0,
                        'wait_timeout'    => 3.0,
                        'heartbeat'       => -1,
                        'max_idle_time'   => 60.0,
                    ],
                ],
            ];
        }
        return $consumers;
    }),
    'providers' => [],
    'drivers'   => [
        'consul' => [
            'uri'   => 'http://127.0.0.1:8500',
            'token' => '',
        ],
        'nacos'  => [
            // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
            'url'          => env('NACOS_URL', 'http://rpc.beedot.cn'),
            // The nacos host info
            // 'host' => '127.0.0.1',
            // 'port' => 8848,
            // The nacos account info
            'username'     => env('NACOS_USERNAME', null),
            'password'     => env('NACOS_PASSWORD', null),
            'guzzle'       => [
                'config' => null,
            ],
            'group_name'   => env('NACOS_GROUP', 'DEFAULT_GROUP'),
            'namespace_id' => env('NACOS_NAMESPACE', 'f47ec671-9703-4a99-a9ee-564a52a39946'),
            'heartbeat'    => 5,
            'ephemeral'    => (bool)env('NACOS_EPHEMERAL', true), // 是否注册临时实例
        ],
    ],
];